package app.page;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.By;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class BasePage {



    public AppiumDriver<MobileElement> driver;

    public BasePage(AppiumDriver<MobileElement> driver) {
        this.driver = driver;
    }

    public void click(By by) {
        try {
            driver.findElement(by).click();
        } catch (Exception e) {
            driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
            driver.findElement(by).click();
            e.printStackTrace();
        }

    }

    public void click(String id) {
        try {
            driver.findElementByAccessibilityId(id).click();
        } catch (Exception e) {
            driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
            driver.findElementByAccessibilityId(id).click();
            e.printStackTrace();
        }

    }

    public void waitSecond() throws InterruptedException {
        Thread.sleep(5000);
    }

    public void sendKeys(By by, String content) {

        driver.findElement(by).sendKeys(content);
    }


    public boolean isDisplayed(By by) {
        return driver.findElement(by).isDisplayed();
    }

    public String getText(By by) {
        return driver.findElement(by).getText();
    }

    public void slideDown() throws InterruptedException {
        int width = driver.manage().window().getSize().getWidth();
        int height = driver.manage().window().getSize().getHeight();
        Thread.sleep(5000);
        new TouchAction(driver).press(PointOption.point(width / 2, height / 2)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(3000))).moveTo(PointOption.point(width / 2, (int)(height*0.2))).release().perform();
    }

}
